package cn.shop.dys.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import cn.shop.lib_common.CommonUtil;
import cn.shop.dys.R;
/**
 *
 * 消息
 * Created by sunjian on 2016/4/19.
 *
 */
public class MessageActivity extends AppCompatActivity implements View.OnClickListener {
    @ViewInject(R.id.lv_message_list)
    private ListView mMessageList;
    @ViewInject(R.id.tv_message_empty)
    private TextView mTvMessageEmpty;
    @ViewInject(R.id.iv_common_back)
    private ImageView mCommonBack;
    @ViewInject(R.id.tv_common_index_titile)
    private TextView mCommonIndexTitle;
    @ViewInject(R.id.iv_common_settings)
    private ImageView mCommonSettings;
    private PopupWindow mPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ViewUtils.inject(this);
        init();
    }

    private void init() {
        initView();
        initEvent();
        initListView();
    }

    private void initView() {
        mCommonIndexTitle.setText("我的消息");
    }

    private void initEvent() {
        mCommonBack.setOnClickListener(this);
        mCommonSettings.setOnClickListener(this);
    }

    private void initListView() {
        List<String> infos = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            infos.add("我是消息" + i);
        }
        mMessageList.setEmptyView(mTvMessageEmpty);
        mMessageList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, infos));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.iv_common_settings:
                initPopupWindow(v);
                break;
        }
    }
    private void initPopupWindow (View view){
        View pwView = View.inflate(this, R.layout.view_popupwindow, null);
        View pwTop=pwView.findViewById(R.id.ll_pw_top);
        View pwBottom=pwView.findViewById(R.id.ll_pw_bottom);
        mPopupWindow = new PopupWindow(pwView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
        int[] location = new int[2];
        view.getLocationInWindow(location);
        mPopupWindow.showAsDropDown(view, (int) -CommonUtil.dpToPx(this, 90), (int) -CommonUtil.dpToPx(this, 0));
        pwTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MessageActivity.this, MessageActivity.class);
                startActivity(intent);
                mPopupWindow.dismiss();
            }
        });
        pwBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MessageActivity.this, MainActivity2.class);
                startActivity(intent);
                mPopupWindow.dismiss();
            }
        });
        //  popupWindow.showAtLocation(view, Gravity.RIGHT| Gravity.TOP, location[0]+40, location[1] + 40);
    }
}
